<?php

namespace app\controller;


use app\facade\SheetXlsx;
use app\model\Devices;
use app\model\Prints;
use app\model\PrintStatus;
use think\facade\Db;
use think\facade\Filesystem;

class PrintsController
{
    protected $middleware = ['AuthCheck'];

    //获取所有打印计划
    public function getPrints($id)
    {
        return rejson(1, ['prints' => Devices::find($id)->prints]);
    }

    //导入打印计划
    public function setPrints()
    {
        if (!request()->has('prtSheet', 'file')) {
            return rejson(0, null, '请上传文件', 412);
        }
        $username = input('post.username');
        $id = input('post.id');
        $prtSheet = request()->file('prtSheet');
        $savePath = 'print/' . date('Y-m-d', time());
        $prtSheetName = Filesystem::disk('excel')->putFile($savePath, $prtSheet, function () {
            return date('YmdHis', time());
        });
        $prtSheetPath = app()->getRootPath() . 'storage/excel/' . $prtSheetName;
        //解析文件数据
        $sheetDatas = SheetXlsx::get($prtSheetPath, 'A', 'F');
        array_shift($sheetDatas);
        //开启事务
        Db::startTrans();
        // A:日期	B:试验任务编号	C:试验任务名称	D:预计使用时长	E:计划完成情况	F:备注
        try {
            $saveDatas = [];
            foreach ($sheetDatas as $data) {
                $saveDatas[] = [
                    'date' => date('Y-m-d', strtotime($data['A'])),
                    'number' => $data['B'],
                    'name' => $data['C'],
                    'expected_duration' => $data['D'],
                    'operator' => $username,
                    'status' => $data['E'],
                    'remarks' => $data['F'],
                    'devices_id' => $id,
                ];
            }

            //保存所有记录
            $Prints = new Prints();
            $prints = $Prints->saveAll($saveDatas);
            //提交事务
            Db::commit();
        } catch (\Exception $e) {
            unlink($prtSheetPath);
            //回滚事务
            Db::rollback();
            return rejson(0, null, '数据添加失败,请重试', 500);
        }
        return rejson(1, ['prints' => $prints]);
    }

    //获取设备打印完成情况
    public function getPrintStatus($id)
    {
        return rejson(1, ['print_status' => Devices::find($id)->printStatus]);
    }

    //导入打印完成情况
    public function printStatus()
    {
        if (!request()->has('prtStuSheet', 'file')) {
            return rejson(0, null, '请上传文件', 412);
        }
        $username = input('post.username');
        $id = input('post.id');
        $prtStuSheet = request()->file('prtStuSheet');
        $savePath = 'printStatus/' . date('Y-m-d', time());
        $prtStuSheetName = Filesystem::disk('excel')->putFile($savePath, $prtStuSheet, function () {
            return date('YmdHis', time());
        });

        $prtStuSheetPath = app()->getRootPath() . 'storage/excel/' . $prtStuSheetName;
        //解析文件数据
        $sheetDatas = SheetXlsx::get($prtStuSheetPath, 'A', 'K');
        array_shift($sheetDatas);
        //开启事务
        Db::startTrans();
        try {
            $saveDatas = [];
            foreach ($sheetDatas as $data) {
                $print = Prints::getByName($data['A']);
                $date = strtotime($data['C']) - strtotime($data['B']);
                $hour = intval($date / 3600);
                $minute = intval(($date / 3600 - $hour) * 60);
                $print->actual_duration = $hour . '小时' . $minute . '分钟';
                $print->status = '已完成';
                $print->save();
                $saveDatas[] = [
                    'name' => $data['A'],
                    'print_s' => $data['B'],
                    'print_e' => $data['C'],
                    'heat_s' => $data['D'],
                    'heat_e' => $data['E'],
                    'line_s' => $data['E'],
                    'line_e' => $data['E'],
                    'machining_s' => $data['E'],
                    'machining_e' => $data['E'],
                    'test_s' => $data['E'],
                    'test_e' => $data['E'],
                    'duty' => $username,
                    'devices_id' => $id
                ];
            }
            $PrintStatus = new PrintStatus();
            $printStatus = $PrintStatus->saveAll($saveDatas);
        } catch (\Exception $e) {
            unlink($prtStuSheetPath);
            //回滚事务
            Db::rollback();
            return rejson(0, $e->getMessage(), '数据添加失败,请重试', 500);
        }
        return rejson(1, ['printStatus' => $printStatus]);
    }

    //上传打印状态文件
    public function importFile()
    {
        $id = input('post.id');
        $printType = input('post.printType');
        if (!request()->has('printFile', 'file')) {
            return rejson(0, null, '请上传文件', 412);
        }
        $printFile = request()->file('printFile');
        $fileName = $printType . '_' . date('YmdHis', time()) . '.' . $printFile->getOriginalExtension();
        $prtStuFileName = Filesystem::disk('public')->putFileAs('printStatus/' . date('Y-m-d', time()), $printFile, $fileName);
        $prtStuPath = 'storage/' . $prtStuFileName;
        $printStatus = PrintStatus::update([
            $printType . '_file' => $prtStuPath,
            'id' => $id
        ]);
        $printT = (string) $printType . '_file';
        return rejson(1, ['filePath' => $printStatus->$printT]);
    }
}